package cn.tx.erp.sys.controller;

import cn.tx.erp.sys.domain.Permission;
import cn.tx.erp.sys.service.PermissionService;
import cn.tx.erp.sys.tree.TreeNode;
import cn.tx.erp.sys.utils.Constast;
import cn.tx.erp.sys.utils.DataGridView;
import cn.tx.erp.sys.utils.ResultObj;
import cn.tx.erp.sys.vo.PermissionVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限管理
 * @author Mxb
 * @version 1.0
 * @date 2020/7/12 10:06
 */
@RequestMapping("/permission")
@RestController
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 加载权限管理左侧权限树
     * @return
     */
    @RequestMapping("/loadPermissionLeftTreeJson")
    public DataGridView loadPermissionLeftTreeJson(PermissionVo permissionVo) {

        QueryWrapper<Permission> qw = new QueryWrapper<>();//
        //// 查询权限类型
        qw.eq("type", Constast.TYPE_MENU);

        List<Permission> list = this.permissionService.list(qw);
        List<TreeNode> treeNodes = new ArrayList<>();
        for (Permission permission : list) {
            Boolean spread = permission.getOpen() == 1 ? true : false;
            treeNodes.add(new TreeNode(permission.getId(), permission.getPid(), permission.getTitle(), spread));
        }
        // 构造返回数据
        return new DataGridView(0, "", 0L, treeNodes);
    }


    /**
     * 记载所有权限
     * @param permissionVo
     * @return
     */
    @RequestMapping("/loadAllPermission")
    public DataGridView loadAllPermission(PermissionVo permissionVo) {
        IPage<Permission> page = new Page<>(permissionVo.getPage(), permissionVo.getLimit());
        QueryWrapper<Permission> qw = new QueryWrapper<>();

        // 模糊查询
        qw.like(StringUtils.isNotBlank(permissionVo.getTitle()), "title", permissionVo.getTitle());
        qw.like(StringUtils.isNotBlank(permissionVo.getPercode()), "percode", permissionVo.getPercode());

        // 查询类型
        qw.eq("type", Constast.TYPE_PERMISSION);

        qw.eq(permissionVo.getId() != null, "id", permissionVo.getId()).or()
                .eq(permissionVo.getId() != null, "pid", permissionVo.getId());

        // 排序方式
        //qw.orderByD("ordernum");
        qw.orderByAsc("ordernum");
        this.permissionService.page(page, qw);

        return new DataGridView(0, "", page.getTotal(), page.getRecords());

    }

    /**
     * 添加权限
     * @param permissionVo
     * @return
     */
    @RequestMapping(value = "/addPermissiion")
    public ResultObj addPermissiion(PermissionVo permissionVo) {
        try {
            //permissionVo.setCreatetime(new Date());
            permissionVo.setType(Constast.TYPE_PERMISSION);
            this.permissionService.save(permissionVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            return ResultObj.ADD_ERR;
        }
    }

    /**
     * 更新权限
     * @param permissionVo
     * @return
     */
    @RequestMapping(value = "/updatePermission")
    public ResultObj updatePermission(PermissionVo permissionVo) {
        try {
            this.permissionService.updateById(permissionVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.UPDATE_ERR;
        }
    }


    /**
     * 删除权限
     * @param permissionVo
     * @return
     */
    @RequestMapping(value = "/deletePermission")
    public ResultObj deletePermission(PermissionVo permissionVo) {
        try {
            this.permissionService.removeById(permissionVo.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.DELETE_ERR;
        }
    }


    //加载最大的排序码
    @RequestMapping("loadPermissionMaxOrderNum")
    public Map<String, Object> loadPermissionMaxOrderNum() {
        Map<String, Object> map = new HashMap<String, Object>();

        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        IPage<Permission> page = new Page<>(1, 1);
        List<Permission> list = this.permissionService.page(page, queryWrapper).getRecords();
        if (list.size() > 0) {
            map.put("value", list.get(0).getOrdernum() + 1);
        } else {
            map.put("value", 1);
        }
        return map;
    }
}
